from django.db import models
from crm.models import Salesperson
import datetime
from crmv2.models import Student

class SurveyTemplate(models.Model):
    name = models.CharField(max_length = 128)
    description = models.CharField(max_length = 1800)

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = 'survey_template'

class Survey(models.Model):
    template = models.ForeignKey(SurveyTemplate)
    owner = models.ForeignKey(Salesperson)
    title = models.CharField(max_length = 128)
    description = models.TextField(blank = True)
    created_at = models.DateField(default = datetime.date.today, editable = False)
    closed = models.BooleanField(default = False)
    n_invitations = models.IntegerField(default = 0, verbose_name = 'invitations')
    n_answers = models.IntegerField(default = 0, verbose_name = 'answers')

    def __unicode__(self):
        return self.title

    def close(self):
        self.closed = True

    class Meta:
        db_table = 'survey'
        ordering = ['-id', ]

class SurveyNotifySchedule(models.Model):
    survey = models.ForeignKey(Survey)
    date = models.DateField()

    class Meta:
        db_table = 'survey_notify_schedule'
        ordering = ['date']

class SurveyInvitation(models.Model):
    survey = models.ForeignKey(Survey)
    interviewee = models.ForeignKey(Student, null = True)
    name = models.CharField(max_length = 32)
    email = models.EmailField()
    answered = models.BooleanField(default = False)
    class Meta:
        db_table = 'survey_invitation'
        ordering = ['name']

class SurveyQuestion(models.Model):
    QUESTION_TYPES = (
        ('T', 'Text'),
        ('S', 'Choice'),
        ('R', 'Radio List'),
        ('C', 'Comment'),
        )

    template = models.ForeignKey(SurveyTemplate)
    index = models.IntegerField()
    type = models.CharField(max_length = 1, choices = QUESTION_TYPES)
    label = models.CharField(max_length = 255)

    def __unicode__(self):
        return self.label

    class Meta:
        db_table = 'survey_question'
        ordering = ['index', ]

class SurveyChoice(models.Model):
    question = models.ForeignKey(SurveyQuestion)
    index = models.IntegerField()
    label = models.CharField(max_length = 255)
    class Meta:
        db_table = 'survey_choice'
        ordering = ['index', ]

class SurveyAnswer(models.Model):
    survey = models.ForeignKey(Survey)
    question = models.ForeignKey(SurveyQuestion)
    answer = models.TextField()
    class Meta:
        db_table = 'survey_answer'
